home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8449 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  72 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Calling a Function Tw
  5. Date: 4 Mar 1996 09:19:59 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hecjv$p7s@gail.ripco.com>
  8. NNTP-Posting-Host: foley.ripco.com
  9.  
  10. razine@aol.com (Razine) in <4he6q9$6r6@newsbf02.news.aol.com> asks:
  11.  
  12. >I am trying to call a function twice from my program on the same printf
  13. >line and have run into a small problem.  i was wondering if someone can
  14. >explain how to fix it.
  15.  
  16. Learn the correct use of the switch construct.  As usual, I have
  17. appended a revision of your code at the EOM, in which my changes and
  18. comments are flagged with `/* mha - ... */'.  And, as usual, some of the
  19. changes reflect judgements about good practice rather than corrections
  20. of errors.
  21.  
  22. [original code snipped]
  23.  
  24. >Now with this function, the printf line will display 0 for the first one
  25. >and garbage for the second one..
  26.  
  27. I don't believe you.  Your original function should have returned 1 for
  28. 0 or 1 as an argument, randomness for all others.
  29.  
  30. #include <stdio.h>              /* mha - added.  Necessary for printf */
  31. int num(int);                   /* mha - added prototype.  Not needed
  32.                                  * in this case, but the habit of
  33.                                  * prototyping functions can prove
  34.                                  * invaluable. */
  35.  
  36. int /* mha - was `void' */ main(void)
  37. {
  38.     printf("First Number is %d , the Second is %d \n", num(0), num(1));
  39.         /* mha - fixed inconsequential typo in above */
  40.     return 0;                   /* mha - added explicit return
  41.                                  * [optional] */
  42. }
  43.  
  44. int num(int index)
  45. {
  46.     int return_number;
  47.     switch (index) {
  48.     case 0:
  49.         return_number = 0;
  50.         break;                  /* mha - added.  Necessary unless you
  51.                                  * want to fall through to the next
  52.                                  * case.  This ain't Pascal. */
  53.     case 1:
  54.         return_number = 1;
  55.         break;                  /* mha - added.  */
  56.     default:                    /* mha - added. Without this,
  57.                                  * return_number which is an
  58.                                  * uninitialized auto variable and will
  59.                                  * contain God-knows-what when index is
  60.                                  * outside of [0,1] */
  61.         return_number = 2;      /* mha - added. */
  62.     }
  63.     return (return_number);
  64.     /* mha - Note that this function body is unnecessarily verbose,
  65.      * even before my additions.  Also note that the above statement can
  66.      * be written `return return_number;' */
  67. }
  68.                                            
  69. --
  70. * Martin Ambuhl       net: mambuhl@ripco.com
  71. * Chicago, IL (USA)    
  72.